﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CSharpDesign.Scripts;
using CSharpDesign.DAL;

namespace CSharpDesign
{
    public partial class DengLu : System.Web.UI.UserControl
    {
        Alert js = new Alert();
        SqlHelper data = new SqlHelper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblyanzheng.Text = CreateRandomCode(4);

                if (Session["UserId"] == null)
                {
                    Login1.Visible = false;
                    Login2.Visible = true;

                }
                else
                {
                    Label1.Text = Session["UserName"].ToString();

                    Login1.Visible = true;
                    Login2.Visible = false;

                }
            }

        }
        protected string CreateRandomCode(int codeCount)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9";
            string[] allCharArray = allChar.Split(',');
            string randomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(10);
                if (temp == t)
                {
                    return CreateRandomCode(codeCount);
                }
                temp = t;
                randomCode += allCharArray[t];
            }
            return randomCode;
        }


        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Response.Redirect("ZhuCe.aspx");

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.Clear();
            Session["UserId"] = null;
            Session["UserName"] = null;
            Response.Redirect("Login.aspx");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserHome.aspx");
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (TxtUserName.Text == "" && TxtPassword.Text == "")
            {
                Alert.AlertAndRedirect("没有输入账号和密码！", "Login.aspx");
            }
            else
            {
                if (lblyanzheng.Text != txtyan.Text)
                {
                    js.Alertjs("验证码错误");
                    return;
                }
                else
                {

                    SqlDataReader dr;
                    dr = data.GetDataReader("select * from  Users where UserName='" + TxtUserName.Text.Trim() + "'and pwd='" + TxtPassword.Text.Trim() + "'");
                    if (dr.Read())
                    {
                        Session["UserId"] = dr["id"].ToString();
                        Session["UserName"] = dr["UserName"].ToString();
                        Session["XingMing"] = dr["XingMing"].ToString();

                        Session["ZheKou"] = dr["ZheKou"].ToString();
                        Response.Redirect("Login.aspx");
                    }
                    else
                    {
                        Alert.AlertAndRedirect("账号或者密码不对请重新登陆！", "Default.aspx");
                    }

                }
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("FindPwd.aspx");

        }
    }

}